/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 d3leaf@126.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.mapperdb.util;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.Assert;

/**
 * spring 工具类
 * @ClassName: SpringUtils 
 * @author huangx
 * @date 2015年2月4日 上午11:57:58
 */
public class SpringUtils {

	/**
	 * @Title: 生产 bean builder
	 * @param beanClass
	 * @return
	 */
	public static BeanDefinitionBuilder genericBean(Class<?> beanClass) {
		Assert.notNull(beanClass);
		return BeanDefinitionBuilder.genericBeanDefinition(beanClass);
	}

	/**
	 * @Title: 生产 bean builder
	 * @param beanClassName
	 * @return
	 */
	public static BeanDefinitionBuilder genericBean(String beanClassName) {
		Assert.notNull(beanClassName);
		return BeanDefinitionBuilder.genericBeanDefinition(beanClassName);
	}

	/**
	 * 向spring容器注册bean
	 * @Title: 
	 * @param beanName
	 * @param beanDefinition
	 * @param context
	 */
	public static void registerBean(ApplicationContext context, String beanName, BeanDefinition beanDefinition) {
		// ApplicationContext 不能为空
		Assert.notNull(context);
		// 断言 bean 没有定义
		Assert.state(!context.containsBean(beanName), "bean [" + beanName + "] is exist.");
		// BeanDefinition 不能为空
		Assert.notNull(beanDefinition);
		// 注册
		ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) context;
		BeanDefinitionRegistry beanDefinitonRegistry = (BeanDefinitionRegistry) configurableApplicationContext.getBeanFactory();
		beanDefinitonRegistry.registerBeanDefinition(beanName, beanDefinition);
	}
}
